/* 
 * Autores:
 *      Jonathan Lopez  11106
 *      Jorge Martínez  11237
 * 
 * Ultima revision: $Rev$
 *      Fecha: $Date$
 * 
 * Archivo: CambiarContrasena.java
 */


package GUI;
import Servicios.*;
import java.util.Arrays;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * Panel para cambiar la contrasena de un usuario.
 * 
 * @author Jorge
 * @author Jonathan
 */
public class CambiarContrasena extends javax.swing.JPanel {
    private final Usuario usuarioActual;
    private final JPanel panelAnterior;
    
    /** Creates new form CambiarContrasena */
    public CambiarContrasena( Usuario usuarioActual ) {
        initComponents();
        
        this.usuarioActual = usuarioActual;
        panelAnterior = FramePrincipal.cambiarPanel(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblAnterior = new javax.swing.JLabel();
        lblPass1 = new javax.swing.JLabel();
        lblPass2 = new javax.swing.JLabel();
        txtAnterior = new javax.swing.JPasswordField();
        txtNuevo1 = new javax.swing.JPasswordField();
        txtNuevo2 = new javax.swing.JPasswordField();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        lblAnterior.setText("Contraseña anterior");

        lblPass1.setText("Nueva contraseña");

        lblPass2.setText("Nueva contraseña de nuevo");

        txtAnterior.setColumns(20);

        txtNuevo1.setColumns(20);

        txtNuevo2.setColumns(20);

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPass2)
                    .addComponent(lblPass1)
                    .addComponent(lblAnterior))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNuevo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNuevo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(340, Short.MAX_VALUE)
                .addComponent(btnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAceptar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAnterior)
                    .addComponent(txtAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPass1)
                    .addComponent(txtNuevo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPass2)
                    .addComponent(txtNuevo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // Revisar que hayan datos
        if( txtAnterior.getPassword().length == 0 ||
            txtNuevo1.getPassword().length == 0 || 
            txtNuevo2.getPassword().length == 0 ){
            
            JOptionPane.showMessageDialog(this, "Debe de ingresar un valor en todas las casillas",
                    "Faltan valores", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // Validar la contrasena nueva
        if( !Arrays.equals(txtNuevo1.getPassword(), txtNuevo2.getPassword()) ){
            JOptionPane.showMessageDialog(this, "Las nuevas contraseñas no coinciden",
                    "Faltan valores", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        
        
        // Cambiar la contrasena del usuario
        String passNuevo = String.copyValueOf( txtNuevo1.getPassword() );
        String passAnterior = String.copyValueOf( txtAnterior.getPassword() );
        Usuario usuarioCambiar = new Usuario( usuarioActual.obtenerUsuario(),
                passAnterior );
        
        if( FramePrincipal.obtenerServicios().cambiarContrasenaUsuario( 
                usuarioCambiar, passNuevo ) )
        {            
            JOptionPane.showMessageDialog(this, "Contraseña cambiada exitosamente",
                    "Contraseña cambiada", JOptionPane.INFORMATION_MESSAGE);
            FramePrincipal.cambiarPanel(panelAnterior);
            return;
        }else{
            JOptionPane.showMessageDialog(this, "Error al cambiar la contraseña",
                    "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        FramePrincipal.cambiarPanel(panelAnterior);
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel lblAnterior;
    private javax.swing.JLabel lblPass1;
    private javax.swing.JLabel lblPass2;
    private javax.swing.JPasswordField txtAnterior;
    private javax.swing.JPasswordField txtNuevo1;
    private javax.swing.JPasswordField txtNuevo2;
    // End of variables declaration//GEN-END:variables
}
